import re

import pymysql
import scrapy
from scrapy.pipelines.images import ImagesPipeline

from news import settings


class NewsPipeline(ImagesPipeline):
    # 下载文章中的图片
    def get_media_requests(self, item, info):
        for img in item['content_images']:
            yield scrapy.Request(img, meta={'content_images': img})

    def file_path(self, request, response=None, info=None):
        content_images = request.meta['content_images']
        content_images = re.sub(r'/1000', '', content_images)  # 因为/后面的都一样不好命名
        content_images = re.sub(r'/0', '', content_images)  # 因为/后面的都一样不好命名
        image_name = content_images.split('/')[-1]
        # 第一种方式
        # 这样写是保存在一个文件夹里面，注意最后是以'.jpg' 结尾
        path = image_name + ".png"
        # 第二种方式
        # path = '%s/%s' % (item['title'], image_name)
        return path


class thumbnail(ImagesPipeline):
    # 下载文章缩略图
    def get_media_requests(self, item, info):
        yield scrapy.Request(item['thumbnail'], meta={'thumbnail': item['thumbnail']})

    def file_path(self, request, response=None, info=None):
        thumbnail = request.meta['thumbnail']
        thumbnail = re.sub(r'/1000', '', thumbnail)  # 因为/后面的都一样不好命名
        thumbnail = re.sub(r'/0', '', thumbnail)  # 因为/后面的都一样不好命名
        image_name = thumbnail.split('/')[-1]
        # 第一种方式
        # 这样写是保存在一个文件夹里面，注意最后是以'.jpg' 结尾
        path = image_name + ".png"
        # 第二种方式
        # path = '%s/%s' % (item['title'], image_name)
        return path


class MysqlPipline(object):
    def __init__(self):
        print("开始连接数据库")
        try:
            self.connect = pymysql.connect(
                host=settings.MYSQL_HOST,
                db=settings.MYSQL_DBNAME,
                user=settings.MYSQL_USER,
                passwd=settings.MYSQL_PASSWD,
                port=settings.MYSQL_PORT,
                charset='utf8',
                use_unicode=True)
        except Exception as error:
            print("链接数据库失败")

        # 通过cursor执行增删查改
        self.cursor = self.connect.cursor();

    def process_item(self, item, spider):

        try:
            thumbnail=item['thumbnail']
            thumbnail = re.sub(r'/1000', '', thumbnail)  # 因为/后面的都一样不好命名
            thumbnail = re.sub(r'/0', '', thumbnail)  # 因为/后面的都一样不好命名
            thumbnail = re.sub(r'https://inews.gtimg.com/(.*?)/', '/zblogimg/', thumbnail)  # 因为/后面的都一样不好命名

            insert_sql = "INSERT INTO news (title,thumbnail,content) VALUES('%s','%s','%s')" % (item['title'], thumbnail+".png", item['content'])
            self.cursor.execute(insert_sql)  # 执行sql语句
        except Exception as error:
            print("sql语句执行失败")
        self.connect.commit()  # 提交保存
        return item
